package com.example.signupservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.commonservice.Common.Result;
import com.example.signupservice.entity.ActivityCheckin;
import com.example.signupservice.service.ActivityCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("signup")
public class ActivityCheckinController {

    @Autowired
    private ActivityCheckinService checkinService;

    /**
     * 签到
     */
    @PostMapping("/checkin")
    public Result<String> checkin(@RequestBody Map<String, Object> params) {
        Long signupId = Long.valueOf(params.get("signupId").toString());
        Long operator = Long.valueOf(params.get("checkinBy").toString());
        boolean success = checkinService.checkin(signupId, Long.valueOf(String.valueOf(operator)));
        return success ? Result.success("签到成功") : Result.error("签到失败");
    }
    /**
     * 签退
     */
    @PostMapping("/checkout")
    public Result<String> checkout(@RequestBody Map<String, Object> params) {
        Long signupId = Long.valueOf(params.get("signupId").toString());
        Long operator = Long.valueOf(params.get("checkoutBy").toString()); // 注意参数名与前端一致
        boolean success = checkinService.checkout(signupId, Long.valueOf(String.valueOf(operator)));
        return success ? Result.success("签退成功") : Result.error("签退失败");
    }

    /**
     * 条件分页查询签到记录
     */
    @GetMapping("/checkin/list")
    public Result<IPage<ActivityCheckin>> listCheckins(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long signupId,
            @RequestParam(required = false) String operator,
            @RequestParam(required = false) String activityName) {

        Map<String, Object> params = new HashMap<>();
        params.put("signupId", signupId);
        params.put("operator", operator);

        return Result.success(checkinService.pageCheckins(current, size, params));
    }

    /**
     * 新增签到记录
     */
    @PostMapping("/checkin/add")
    public Result<String> addCheckin(@RequestBody ActivityCheckin checkin) {
        boolean success = checkinService.addCheckinRecord(checkin);
        return success ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 删除签到记录
     */
    @PostMapping("/checkin/delete")
    public Result<String> deleteCheckin(@RequestParam Long id) {
        boolean success = checkinService.deleteCheckinRecord(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 更新签到记录
     */
    @PostMapping("/checkin/update")
    public Result<String> updateCheckin(@RequestBody ActivityCheckin checkin) {
        boolean success = checkinService.updateCheckinRecord(checkin);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    /**
     * 获取单个签到记录
     */
    @GetMapping("/checkin/get")
    public Result<ActivityCheckin> getCheckin(@RequestParam Long id) {
        ActivityCheckin checkin = checkinService.getCheckinRecord(id);
        return checkin != null ? Result.success(checkin) : Result.error("记录不存在");
    }

    @GetMapping("/checkin/detail")
    public Result<ActivityCheckin> getCheckinDetail(@RequestParam Long signupId) {
        ActivityCheckin checkin = checkinService.getCheckinDetail(signupId);
        return checkin != null ? Result.success(checkin) : Result.error("记录不存在");
    }

    @PostMapping("/evaluate")
    public Result<String> evaluateActivity(@RequestBody Map<String, Object> params) {
        Long checkinId = Long.valueOf(params.get("checkinId").toString());
        Integer score = Integer.valueOf(params.get("score").toString());
        String comment = params.get("comment") != null ? params.get("comment").toString() : null;
        boolean success = checkinService.updateEvaluation(checkinId, score, comment);
        return success ? Result.success("评价成功") : Result.error("评价失败");
    }
}
